﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class LoadBLL
    {
        private static readonly ICarrierDA carrierDA = DataAccess.CreateCarrierDA();
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly IEquipmentTypeDA equipmentTypeDA = DataAccess.CreateEquipmentTypeDA();
        private static readonly ILoadDetailDA loadDetailDA = DataAccess.CreateLoadDetailDA();
        private static readonly ILoadHeaderDA loadHeaderDA = DataAccess.CreateLoadHeaderDA();
        private static readonly ILoadPickDetailDA loadPickDetailDA = DataAccess.CreateLoadPickDetailDA();
        private static readonly IOrderDetailDA orderDetailDA = DataAccess.CreateOrderDetailDA();
        private static readonly IOrdersDA ordersDA = DataAccess.CreateOrdersDA();
        private PubParasInfo pubParasInfo;
        private static readonly ITransportEquipmentDA transportEquipmentDA = DataAccess.CreateTransportEquipmentDA();

        public LoadBLL()
        {
            this.pubParasInfo = null;
        }

        public LoadBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public LoadBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string DeleteLoadDetailInfo(string loadID, List<string> lineID)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string temp = string.Empty;
            try
            {
                if (loadDetailDA.DeleteLoadDetailByLineID(dataBase, tran, loadID, lineID, this.dataBaseID[0]) > 0)
                {
                    temp = loadID;
                }
                if (!string.IsNullOrEmpty(temp))
                {
                    temp = temp + "/";
                    for (int n = 0; lineID.Count > n; n++)
                    {
                        temp = temp + lineID[n] + ",";
                    }
                    temp = temp.Substring(0, temp.Length - 1);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public string DeleteLoadInfo(List<string> IDS)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string temp = string.Empty;
            try
            {
                if (loadHeaderDA.DeleteLoadHeader(dataBase, tran, IDS, this.dataBaseID[0]) > 0)
                {
                    if (loadDetailDA.DeleteLoadDetail(dataBase, tran, IDS, this.dataBaseID[0]) > 0)
                    {
                    }
                    for (int n = 0; IDS.Count > n; n++)
                    {
                        temp = temp + IDS[n] + ",";
                    }
                    temp = temp.Substring(0, temp.Length - 1);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public DataSet GetAllLoad()
        {
            return loadHeaderDA.GetAllLoadHeader(this.dataBaseID[0]);
        }

        public LoadHeaderInfo GetLoadByID(string id)
        {
            LoadHeaderInfo loadHeaderInfo = loadHeaderDA.GetLoadHeaderByID(id, this.dataBaseID[0]);
            if (loadHeaderInfo != null)
            {
                loadHeaderInfo.LoadDetailInfos = loadDetailDA.GetLoadDetailByID(id, this.dataBaseID[0]);
                loadHeaderInfo.LoadPickDetailInfos = loadPickDetailDA.GetLoadPickDetailByID(id, this.dataBaseID[0]);
            }
            return loadHeaderInfo;
        }

        public DataSet GetLoadByQueryEntity(LoadHeaderQueryEntity loadHeaderQueryEntity)
        {
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(loadHeaderQueryEntity.LoadID))
            {
                sqlWhere.Add(" LH.LOAD_ID " + loadHeaderQueryEntity.LoadID);
            }
            if (!string.IsNullOrEmpty(loadHeaderQueryEntity.ExternalID))
            {
                sqlWhere.Add(" LH.EXTERNAL_ID " + loadHeaderQueryEntity.ExternalID);
            }
            if (!string.IsNullOrEmpty(loadHeaderQueryEntity.Status))
            {
                sqlWhere.Add(" LH.STATUS " + loadHeaderQueryEntity.Status);
            }
            if (!string.IsNullOrEmpty(loadHeaderQueryEntity.DepartureTime))
            {
                sqlWhere.Add(loadHeaderQueryEntity.DepartureTime.Replace("@DepartureTime@", "LH.DEPARTURE_TIME"));
            }
            if (!string.IsNullOrEmpty(loadHeaderQueryEntity.CarrierID))
            {
                sqlWhere.Add(" LH.CARRIER_ID " + loadHeaderQueryEntity.CarrierID);
            }
            if (!string.IsNullOrEmpty(loadHeaderQueryEntity.VehicleNumber))
            {
                sqlWhere.Add(" LH.VEHICLE_NUMBER " + loadHeaderQueryEntity.VehicleNumber);
            }
            if (!string.IsNullOrEmpty(loadHeaderQueryEntity.Route))
            {
                sqlWhere.Add(" LH.ROUTE " + loadHeaderQueryEntity.Route);
            }
            if (!string.IsNullOrEmpty(loadHeaderQueryEntity.Door))
            {
                sqlWhere.Add(" LH.DOOR " + loadHeaderQueryEntity.Door);
            }
            if (!string.IsNullOrEmpty(loadHeaderQueryEntity.CreatedBy))
            {
                sqlWhere.Add(" LH.CREATED_BY " + loadHeaderQueryEntity.CreatedBy);
            }
            if (!string.IsNullOrEmpty(loadHeaderQueryEntity.CreatedDate))
            {
                sqlWhere.Add(loadHeaderQueryEntity.CreatedDate.Replace("@CreatedDate@", "LH.CREATED_DATE"));
            }
            return loadHeaderDA.GetLoadHeaderByQueryList(sqlWhere, loadHeaderQueryEntity, this.dataBaseID[0], this.pubParasInfo);
        }

        public string InsertLoadDetailInfo(string orderID, string loadID, string lineID)
        {
            string whLoginID = this.dataBaseID[0];
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string LoadID = string.Empty;
            try
            {
                if (loadDetailDA.CheckLoadDetailIDOrderIDUnique(loadID, orderID, whLoginID))
                {
                    throw new Exception("此发货订单在本车已装车!");
                }
                LoadDetailInfo loadDetailInfo = loadDetailDA.GetLoadDetailInfoByOrderID(orderID, whLoginID);
                loadDetailInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                loadDetailInfo.Status = "New";
                loadDetailInfo.LoadID = loadID;
                loadDetailInfo.LineID = int.Parse(lineID);
                loadDetailInfo.ExternalShipmentID = "";
                loadDetailInfo.OhType = "";
                loadDetailInfo.FlowOrderID = "";
                loadDetailInfo.TotalCost = 0.0;
                loadDetailInfo.TotalValue = 0.0;
                loadDetailInfo.CreatedBy = this.pubParasInfo.UserID;
                loadDetailInfo.UpdatedBy = loadDetailInfo.CreatedBy;
                if (loadDetailDA.InsertLoadDetail(dataBase, tran, loadDetailInfo, whLoginID) > 0)
                {
                    LoadID = loadDetailInfo.LoadID + "/" + loadDetailInfo.LineID;
                    new AuditDataBLL().InsertAuditData<LoadDetailInfo>(dataBase, tran, null, loadDetailInfo, AuditAction.Insert, this.pubParasInfo.UserID, "LOAD_DETAIL", this.dataBaseID[0]);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return LoadID;
        }

        public string InsertLoadInfo(LoadHeaderInfo loadHeaderInfo)
        {
            if (!string.IsNullOrEmpty(loadHeaderInfo.CarrierID) && !carrierDA.CheckCarrierIDUnique(loadHeaderInfo.CarrierID, this.dataBaseID[0]))
            {
                throw new Exception("承运商不存在,请重新输入!");
            }
            if (!string.IsNullOrEmpty(loadHeaderInfo.LoadID) && loadHeaderDA.CheckLoadHeaderIDUnique(loadHeaderInfo.LoadID, this.dataBaseID[0]))
            {
                throw new Exception("此装车单号已存在!");
            }
            string whLoginID = this.dataBaseID[0];
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string LoadID = string.Empty;
            try
            {
                if (string.IsNullOrEmpty(loadHeaderInfo.LoadID))
                {
                    loadHeaderInfo.LoadID = commonMethod.GenNextNumStr(dataBase, tran, "LOAD_HEADER", whLoginID);
                }
                loadHeaderInfo.CreatedBy = this.pubParasInfo.UserID;
                loadHeaderInfo.UpdatedBy = loadHeaderInfo.CreatedBy;
                if (loadHeaderDA.InsertLoadHeader(dataBase, tran, loadHeaderInfo, whLoginID) > 0)
                {
                    if ((loadHeaderInfo.LoadDetailInfos != null) && (loadHeaderInfo.LoadDetailInfos.Count > 0))
                    {
                        foreach (LoadDetailInfo DetailInfo in loadHeaderInfo.LoadDetailInfos)
                        {
                            DetailInfo.LoadID = loadHeaderInfo.LoadID;
                            DetailInfo.CreatedBy = this.pubParasInfo.UserID;
                            DetailInfo.UpdatedBy = DetailInfo.CreatedBy;
                            loadDetailDA.InsertLoadDetail(dataBase, tran, DetailInfo, whLoginID);
                        }
                    }
                    LoadID = loadHeaderInfo.LoadID;
                    new AuditDataBLL().InsertAuditData<LoadHeaderInfo>(dataBase, tran, null, loadHeaderInfo, AuditAction.Insert, this.pubParasInfo.UserID, "LOAD_HEADER", this.dataBaseID[0]);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return LoadID;
        }

        public string UpdateLoadAndOrderStatusByLoadID(string status, string loadID)
        {
            return loadHeaderDA.UpdateLoadAndOrderStatusByLoadID(status, loadID, this.pubParasInfo.UserID, this.dataBaseID[0]);
        }

        public string UpdateLoadInfo(LoadHeaderInfo loadHeaderInfo)
        {
            if (!string.IsNullOrEmpty(loadHeaderInfo.CarrierID) && !carrierDA.CheckCarrierIDUnique(loadHeaderInfo.CarrierID, this.dataBaseID[0]))
            {
                throw new Exception("承运商不存在,请重新输入!");
            }
            string whLoginID = this.dataBaseID[0];
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string LoadID = string.Empty;
            try
            {
                loadHeaderInfo.UpdatedBy = this.pubParasInfo.UserID;
                LoadHeaderInfo oldLoad = loadHeaderDA.GetLoadHeaderByID(dataBase, tran, loadHeaderInfo.LoadID, whLoginID);
                if (loadHeaderDA.UpdateLoadHeader(dataBase, tran, loadHeaderInfo, whLoginID) > 0)
                {
                    LoadID = loadHeaderInfo.LoadID;
                    new AuditDataBLL().InsertAuditData<LoadHeaderInfo>(dataBase, tran, oldLoad, loadHeaderInfo, AuditAction.Update, this.pubParasInfo.UserID, "LOAD_HEADER", this.dataBaseID[0]);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return LoadID;
        }
    }
}

